<?php

/**
* 品牌管理模型
*/
class BrandModel extends Model{
    public $table='brand';
    
    public $validate=array(
        array('bname','nonull','品牌名称不能为空',2,3),
        array('logo','nonull','未上传logo图片',2,3)
    );
    
    /*添加品牌
     **/
    public function addBrand(){
        if(!$this->create()) return false;
		$logoTemp=Q('post.logo');
		$this->data['logo']=str_replace('LogoTemp', 'Logo',Q('post.logo'));
	    is_dir('Upload/Logo/')||mkdir('Upload/Logo/',0777,true);
		is_file($logoTemp)&&copy($logoTemp,$this->data['logo']);
		return $this->add();
    }
	
	/*修改品牌信息
	 * 更新信息后删除之前的logo
	 **/
	public function editBrand(){
	    if(!$this->create()) return false;
		$this->imgFile();
		$bid=Q('post.bid',0,'intval');
		//获得旧logo路径，删除旧logo,这里要用M函数不能用$this->
		$oldLogo=M('brand')->where("bid={$bid}")->find();
		is_file($oldLogo['logo'])&&unlink($oldLogo['logo']);
		
		$logoTemp=Q('post.logo');
		$this->data['logo']=str_replace('LogoTemp', 'Logo',Q('post.logo'));
	    is_dir('Upload/Logo/')||mkdir('Upload/Logo/',0777,true);
		//与下行不能互换，报错:未定义索引logo
		is_file($logoTemp)&&copy($logoTemp,$this->data['logo']);
		$this->update();
		return true;
	}
	
	
	/*品牌排序
	 **/
	public function toSort(){
	    foreach (Q('post.') as $k=> $b) {
    		$this->where("bid={$k}")->update(array('sort'=>$b));
		}
	}
	
	
	
	
	
	
	
	
}
























    
    
    
?>